package com.bingxu.flink.chapter02.transformation;

import com.bingxu.flink.bean.WaterSensor;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;
import java.util.List;


/**
 * @author :BingXu
 * @description :TODO
 * @date :2021/8/9 11:34
 * @modifier :
 */

public class Trans_Flatmap {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        DataStreamSource<String> source = env.socketTextStream("localhost", 8989);
        DataStreamSource<List<Integer>> source = env.fromElements(Arrays.asList(1, 2, 43, 23, 545, 678, 78, 90));

        SingleOutputStreamOperator<Integer> flatMapDS = source.flatMap(new FlatMapFunction<List<Integer>, Integer>() {
            @Override
            public void flatMap(List<Integer> integers, Collector<Integer> collector) throws Exception {
                for (Integer integer : integers) {
                    collector.collect(integer%2==0?integer/2:integer);
                }
            }
        });
        flatMapDS.print();

        env.execute();

    }
}
